#!/usr/bin/env python
"""linkdir.py -- symbolic links from one dir to another
    converting filenames to numbers
"""

import os
import sys
from os.path import join

def main(sourcedir, targetdir=""):
    targetdir = targetdir if targetdir else sourcedir + "-link"
    if (os.access(sourcedir, os.R_OK) and
        (os.access(targetdir, os.W_OK) or os.mkdir(targetdir))):
        for i, file in enumerate(os.listdir(sourcedir)):
            source = join(sourcedir, file)
            target = join(targetdir, str(i))
            os.symlink(join(source), join(target))
            #print("joined " + source + " to " + target)

if __name__ == "__main__":
    try:
        sys.exit(main(*sys.argv[1:]))
    except TypeError:
        print("Usage: linkdir.py sourcedir linkdir")
        sys.exit(1)
